package com.wn.web;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wn.common.annotation.RequireRoles;
import com.wn.common.pojo.Appointment;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Rsp;
import com.wn.common.pojo.User;
import com.wn.common.util.Const;
import com.wn.common.util.JWTUtil;
import com.wn.service.AppointmentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestControllerAdvice
@RequestMapping("appointment")
@Api(value="预约接口",description = "预约接口")
@Slf4j
public class AppointmentController{

    @Autowired
    private AppointmentService appointmentService;


    /**
     * 条件查询接口
     * @param pageParam
     * @return
     */
    @PostMapping("queryByCondition")
    @RequireRoles(roles = "super_admin",index = 0,type=Appointment.class)
    public Rsp queryByCondition(@RequestHeader("token") String token,
                                @RequestBody PageParam<Appointment> pageParam){

        return appointmentService.queryByCondition(pageParam);

    }

    /**
     * 添加接口
     * @param
     * @return@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd",
     *                             timezone = "GMT-8")
     */
    @PostMapping("createAppointment")
    @RequireRoles(roles = {"super_admin","admin","member"},index = 1,type=Appointment.class)
    public Rsp insert(@RequestBody @JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd")
                                  Appointment appointment,
                      @RequestHeader("token") String token) throws Exception {
        log.info("预约对象属性：{}",appointment);
        User user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return appointmentService.createAppointment(appointment,user);
    }

    /**
     * 删除接口
     * @param appointment
     * @return
     */
    @RequireRoles(roles = {"super_admin","admin"},index = 1,type=Appointment.class)
    public Rsp delete(@RequestBody Appointment appointment,
                      @RequestHeader("token") String token){

        return appointmentService.delete(appointment.getId());
    }

    /**
     * 修改接口
     * @param appointment
     * @return
     */
    @PostMapping("update")
    @RequireRoles(roles = {"super_admin","admin"},index = 1,type=Appointment.class)
    public Rsp update(@RequestBody @Valid Appointment appointment,
                      @RequestHeader("token") String token){
        return appointmentService.update(appointment);

    }

    @PostMapping("queryById")
    public Rsp queryById(@RequestBody Appointment condition){
       return appointmentService.queryById(condition.getId());

    }
    /*查询订单详情*/
    @PostMapping("queryByOrderNumber")
    @RequireRoles(roles = {"super_admin","admin","member"},index = 1,type=Appointment.class)
    public Rsp queryByOrderNumber(@RequestBody Appointment appointment,
                                  @RequestHeader("token") String token){
        return appointmentService.queryByOrderNumber(appointment.getOrderNumber());

    }

    /*用户进入个人中心查询所有预约订单详情*/
    @PostMapping("getAppointmentDetailByUserInfo")
    @RequireRoles(roles = {"super_admin","admin","member"},index = 0,type=Appointment.class)
    public Rsp queryByOrderNumber(@RequestHeader("token") String token) throws Exception {

        return appointmentService.getAppointmentDetailByUserInfo(token);

    }

    /*管理员修改预约订单状态至待付款*/
    @PostMapping("changeStatusToPay")
    @RequireRoles(roles = {"super_admin","admin"},index = 1,type=Appointment.class)
    public Rsp changeStatusToPay(@RequestBody Appointment appointment,@RequestHeader("token") String token) throws Exception {
        log.info("预约单金额和订单编号,{},{}",appointment.getOrderNumber(),appointment.getResPrice());
        return appointmentService.changeStatusToPay(appointment);

    }

}
